import axios from "axios";
import {ElMessage} from "element-plus";
import 'element-plus/dist/index.css'
import {useBaseStore} from "@/stores/useBaseStore.js";

let baseStore =null;

axios.defaults.baseURL = 'http://127.0.0.1:3000';
axios.interceptors.request.use(
  function (config) {
    if (baseStore == null) {
      baseStore =useBaseStore();
    }
    config.headers.Authorization = 'Bearer ' + baseStore.token;
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
)
axios.interceptors.response.use(
  function (res){
    if (res.data.status >= 400){
      ElMessage.error({
        message: res.data.msg,
      })
      return Promise.reject(res.data);
    }
    return res.data.data;
  },
  function (error) {
    ElMessage.error({
      message:"请求失败，请稍后重试",
    });
    return Promise.reject(error);
  }
);

export default axios;
